<?php
	/**
	 *  注册
	 */
    class Register extends APIBase {
		
		public $tag = "Register";
		public $isLogin = false;
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			return true;
		}

		public function logic() {
			
			if (!isset($this->param['user'])) {
				$this->returnError(310, "请输入用户名！");
			}
			
			if (!isset($this->param['password'])) {
				$this->returnError(310, "请输入密码！");
			}
			
			if (!isset($this->param['platform'])||$this->param['platform']=='') {
				$this->returnError(310, "平台不能为空！");
			}
			
			if (!isset($this->param['puid'])||$this->param['puid']=='') {
				$this->returnError(310, "机器码不能为空！");
			}
			
			if (!isset($this->param['sex'])) {
				$this->param['sex'] = 1;
			}
		
			if (!isset($this->param['vercode'])) {
				$vercode = "";
			}else if(strlen($this->param['vercode']) > 30){
				$vercode = substr($this->param['vercode'],0,30);
			}else{
				$vercode = $this->param['vercode'];
			}

			if (!isset($this->param['ua'])) {
				$ua = "";
			}else if(strlen($this->param['ua']) > 20){
				$ua = substr($this->param['ua'],0,20);
			}else{
				$ua = $this->param['ua'];
			}

			if (!isset($this->param['imsi'])) {
				$imsi = "";
			}else if(strlen($this->param['imsi']) > 20){
				$imsi = substr($this->param['imsi'],0,20);
			}else{
				$imsi = $this->param['imsi'];
			}

			if (!isset($this->param['imei'])) {
				$imei = "";
			}else if(strlen($this->param['imei']) > 20){
				$imei = substr($this->param['imei'],0,20);
			}else{
				$imei = $this->param['imei'];
			}

			if (!isset($this->param['cid'])) {
				$cid = "";
			}else if(strlen($this->param['cid']) > 20){
				$cid = substr($this->param['cid'],0,20);
			}else{
				$cid = $this->param['cid'];
			}

			if (!isset($this->param['channel'])) {
				$channel = "00000";
			}else if(strlen($this->param['channel']) > 20){
				$channel = substr($this->param['channel'],0,20);
			}else{
				$channel = $this->param['channel'];
			}
		
			$user_reg = '/[a-zA-Z0-9]{4,20}/';
			$password_reg = '/[a-zA-Z0-9]{4,20}/';
			if (!preg_match($user_reg,$this->param['user'])) {
				$this->returnError(311, '账号长度为4-20位，包含英文、数字，请重新填写');
			}
			if (!preg_match($password_reg,$this->param['password'])) {
				$this->returnError(312, '密码最长为4-20位，包含英文、数字，请重新填写');
			}
			
			$where = array('user' => strtolower($this->param['user']));
			$row = $this->mysql->select("player", "COUNT(*) AS `count`", $where);
			if ($row[0]['count'] >= 1) {
				$this->returnError(313, '用户名已经存在！');
			}
			
			$new_user = $this->createUser($vercode,$ua,$imsi,$imei,$cid,$channel);
			$uid = $this->mysql->insert("player", $new_user);
			if ($uid == 0) {
				$this->e("insert user error");
				$this->returnError(500, "server is error");
			}
			
			$this->initDataRedis($uid);
			$new_user['id'] = $uid;
			$ret = $this->data_redis->hMset("hu:{$uid}", $new_user);
			if (!$ret) {
				$this->e("redis hMset error.");
				$this->returnError(3, "redis hMset error.");
			}
			
			$title = "欢迎您来到" . Game::$appname;
			$content = "欢迎您来到" . Game::$appname . "首次登陆赠送" . Game::$init_money . "金币，连续每日登陆，还能获得更多免费金币，首次充值也会赠首充礼包哦！";
			$this->sendMessage($uid, $title, $content);
			$user_info = $this->data_redis->hMget("hu:$uid", array(
					'user', 'name', 'sex', 'money', 'coin', 'skey',
					'login_days', 'is_get', 'total_board', 'total_win', 'exp', 
					'bank', 'password', 'is_set_bank_password', 'avater_url',
					'gift_num1','gift_num2','gift_num3','gift_num4','charisma','activity',
					'isget_coin_task_reward_record','coin_task_complete_record',
                    'changeCardtoolNum','betsDoubletoolNum','banComparetoolNum','is_vip'
			));
			
			$user_info['uid'] = (int)$uid;
			$user_info['sex'] = (int)$user_info['sex'];
			$user_info['money'] = (int)$user_info['money'];
			$user_info['bank'] = (int)$user_info['bank'];
			$user_info['is_set_bank_password'] = (int)$user_info['is_set_bank_password'];
			$user_info['password'] = $user_info['password'];
			$user_info['coin'] = (int)$user_info['coin'];
			$user_info['login_days'] = (int)$user_info['login_days'];
			$user_info['is_get'] = (int)$user_info['is_get'];
			$total_lose = $user_info['total_board'] - $user_info['total_win'];
			$user_info['history'] = array((int)$user_info['total_win'], (int)$total_lose);
			$user_info['total_board'] = (int)$user_info['total_board'];
			$user_info['total_win'] = (int)$user_info['total_win'];
			$user_info['exp'] = (int)$user_info['exp'];
			$user_info['is_vip'] = (int)$user_info['is_vip'];
			$user_info['gift_num1'] = (int)$user_info['gift_num1'];
			$user_info['gift_num2'] = (int)$user_info['gift_num2'];
			$user_info['gift_num3'] = (int)$user_info['gift_num3'];
			$user_info['gift_num4'] = (int)$user_info['gift_num4'];
			$user_info['charisma'] = (int)$user_info['charisma'];
			$user_info['activity'] = (int)$user_info['activity'];
			$user_info["isget_coin_task_reward_record"] = (int)$user_info['isget_coin_task_reward_record'];//是否领取奖券任务奖励记录
            $user_info['changeCardtoolNum'] = (int)$user_info['changeCardtoolNum'];
            $user_info['betsDoubletoolNum'] = (int)$user_info['betsDoubletoolNum'];
            $user_info['banComparetoolNum'] = (int)$user_info['banComparetoolNum'];
			$user_info['ip'] = Config::$ip_server_confing['ip'];
			$user_info['port'] = Config::$ip_server_confing['port'];
			//剩余天数
			$user_info['remaining'] = 0;
			$vga = $this->cache_redis->hGetall("vga");
			$user_info['upvenus'] = $vga['upvenus'];
			$user_info['upgoods'] = $vga['upgoods'];
				
			$this->returnData($user_info);
		}
		

		private function createUser($vercode,$ua,$imsi,$imei,$cid,$channel) {
			$now = time();
			$salt = $this->get_salt(6);
			$user_info = array();
			$user_info["user"] = $this->param['user'];
			$user_info["name"] = $this->param['name'];
			$user_info["platform_type"] = $this->param['platform'];
			$user_info["puid"] = $this->param['puid'];
			$user_info["password"] = md5($this->param['password'] . $salt);
			$user_info["salt"] = $salt;
			$user_info["status"] = 1; // 0：为封号的
			$user_info["sex"] = $this->param['sex'];
			//$user_info["money"] = Game::$init_money;
			$user_info["money"] = 9000;
			$user_info["coin"] = Game::$init_coin;
			$user_info["skey"] = md5(time().$salt);
			$user_info["login_days"] = 0;
			$user_info["is_get"] = 0;
			$user_info["total_board"] = 0;
			$user_info["total_win"] = 0;
			$user_info["vercode"] = $vercode;
			$user_info["ua"] = $ua;
			$user_info["imsi"] = $imsi;
			$user_info["imei"] = $imei;
			$user_info["cid"] = $cid;
			$user_info["channel"] = $channel;
			$user_info["exp"] = 0;
			$user_info["heartbeat_at"] = $now;
			$user_info["broke_num"] = 0;
			$user_info["broke_time"] = 0;
			$user_info["create_time"] = $now;
			$user_info["update_time"] = $now;
			$user_info["bank"] = 0;
			$user_info["bank_password"] = md5($this->param['password'] . $salt);
			$user_info["bank_salt"] = $salt;
			$user_info["is_set_bank_password"] = 1;
			$user_info["avater_url"] = "";
			$user_info["gift_num1"] = 0; //礼物1 鸡蛋
			$user_info["gift_num2"] = 0; //礼物2 鲜花
			$user_info["gift_num3"] = 0; //礼物3 汽车
			$user_info["gift_num4"] = 0; //礼物4 游艇
			$user_info["charisma"] = 0;  //魅力属性值
			$user_info["activity"] = 0; //活跃度属性
			$user_info["isget_coin_task_reward_record"] = 0;//是否领取奖券任务奖励记录
			$user_info["coin_task_complete_record"] = "0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0";
            $user_info["changeCardtoolNum"] = 2;
            $user_info["betsDoubletoolNum"] = 2;
            $user_info["is_vip"] = 0;
			return $user_info;
		}
		
		private function get_salt($len) {
			$salt = Game::$salt_password;
			return $salt;
		}
		
		public function after() {
    		$this->deinitDataRedis();
			$this->deinitCacheRedis();
			$this->deinitMysql();
		}
    }
?>
